

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.HelpRequestBeanRemote;

/**
 * Servlet implementation class UnregisteredHelpServlet
 */
public class UnregisteredHelpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UnregisteredHelpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String receiver = request.getParameter("receiver");
		String ability = request.getParameter("ability");
		String place = request.getParameter("place");
		String description = request.getParameter("description");
		String retribution = request.getParameter("retribution");
		String eMail = request.getParameter("eMail");
		String sender = "anonymous".concat(eMail);
		HelpRequestBeanRemote helpBean;
		InitialContext context;
		String requestOutcome = null;
		String error = "Error in sending the help request";
		
		if ((receiver != null) && (ability != null) && (place != null) && (description != null) && (retribution != null)) {
			try {
				context = new InitialContext();
				Object ref = context.lookup("HelpRequestBeanRemote");
				helpBean = (HelpRequestBeanRemote) PortableRemoteObject.narrow(ref,HelpRequestBeanRemote.class);
				float pay = Float.parseFloat(retribution);
				requestOutcome = helpBean.sendHelpRequest(sender, receiver, ability, place, description, pay);
			} catch (NamingException e) {
				System.out.println("Naming Exception");
				e.printStackTrace();
			} catch (NumberFormatException e) {
				request.getSession().setAttribute("helprequestoutcome",error);
				response.sendRedirect("UnregisteredHelpRequestPage.jsp");
				return;
			}
			System.out.print("Va");
		} else {
			request.getSession().setAttribute("helprequestoutcome",error);
			response.sendRedirect("UnregisteredHelpRequestPage.jsp");
			return;
		}
		if(requestOutcome.equals(error)){
			request.getSession().setAttribute("helprequestoutcome",requestOutcome);
			response.sendRedirect("UnregisteredHelpRequestPage.jsp");
		} else {
			response.sendRedirect("UnregisteredProfilePage.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
